package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * 抽象基类
 * 
 * @author 演示
 * 
 */
public abstract class BaseServlet extends HttpServlet {

	private static final long serialVersionUID = 8196938314965941620L;

	protected void process(HttpServletRequest request, HttpServletResponse response, String path)
			throws ServletException, IOException {

		RequestDispatcher rd = request.getRequestDispatcher(path);
		rd.forward(request, response);

	}

	protected void error(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
		rd.forward(request, response);

	}

	public String write(Object word, HttpServletRequest request, HttpServletResponse response) {
		try {
			// String wordJSON = JSON.toJSONString(word);
			PrintWriter out = response.getWriter();
			out.println(word);
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println("write出错：" + e);
		}
		return null;
	}

	public String Appwrite(Object word, HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			String wordJSON = JSON.toJSONString(word);
			PrintWriter out = response.getWriter();
			out.println(wordJSON);
			out.flush();
			out.close();
		} catch (Exception e) {
			System.out.println("write出错：" + e);
		}
		return null;
	}

}
